]> git.r.bdr.sh - rbdr/map/blame - Map/Presentation/Base Components/MapRender/MapVertices.swift
Add some debouncing
[rbdr/map] / Map / Presentation / Base Components / MapRender / MapVertices.swift
CommitLineData
be897af3 1// Copyright (C) 2024 Rubén Beltrán del Río
98f09799 2
be897af3
RBR
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
98f09799 7
be897af3
RBR
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
98f09799 12
be897af3
RBR
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see https://map.tranquil.systems.
5e8ff485
RBR
15import SwiftUI
16
17struct MapVertices: View {
18
5e8ff485
RBR
19 let mapSize: CGSize
20 let vertexSize: CGSize
21 let vertices: [Vertex]
fdb4633d 22 let padding = CGFloat(5.0)
5e8ff485 23
e2c37ac1
RBR
24 var onDragVertex: (Vertex, CGFloat, CGFloat) -> Void = { _, _, _ in }
25
5e8ff485 26 var body: some View {
fdb4633d
RBR
27 ZStack(alignment: .topLeading) {
28 ForEach(vertices, id: \.id) { vertex in
e2c37ac1 29 ZStack(alignment: .topLeading) {
be897af3
RBR
30 getVertexShape(vertex).fill(Color.Map.vertexColor)
31 Text(vertex.label.replacingOccurrences(of: "\\n", with: "\n")).font(.Theme.vertexLabel)
32 .foregroundColor(.Map.labelColor)
fdb4633d
RBR
33 .shadow(color: .white, radius: 0, x: -0.5, y: -0.5)
34 .shadow(color: .white, radius: 0, x: 0.5, y: 0.5)
35 .offset(
e2c37ac1
RBR
36 CGSize(
37 width: w(vertex.position.x) + vertexSize.width + padding,
38 height: h(vertex.position.y) + 7.0))
39 }.gesture(
40 DragGesture()
41 .onChanged { value in
42 let deltaX = value.startLocation.x - value.location.x
43 let deltaY = value.startLocation.y - value.location.y
44 onDragVertex(vertex, deltaX, deltaY)
45 }
46 )
fdb4633d 47 }
5e8ff485
RBR
48 }
49 }
50
51 func h(_ dimension: CGFloat) -> CGFloat {
52 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
53 }
54
55 func w(_ dimension: CGFloat) -> CGFloat {
56 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
57 }
77d0155b
RBR
58
59 func getVertexShape(_ vertex: Vertex) -> Path {
60 switch vertex.shape {
61 case .circle:
62 return Path { path in
63 path.addEllipse(
64 in: CGRect(
65 origin: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)), size: vertexSize
66 ))
67 }
68 case .square:
69 return Path { path in
70 path.addRect(
71 CGRect(
72 x: w(vertex.position.x), y: h(vertex.position.y), width: vertexSize.width,
73 height: vertexSize.height
74 ))
75 }
76 case .triangle:
77 return Path { path in
78 path.move(to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
79 path.addLine(
80 to: CGPoint(
81 x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y) + vertexSize.height)
82 )
83 path.addLine(
84 to: CGPoint(x: w(vertex.position.x) + vertexSize.width / 2.0, y: h(vertex.position.y)))
85 path.addLine(
86 to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
87 path.closeSubpath()
88 }
89 case .x:
90 return Path { path in
91 path.move(to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)))
92 path.addLine(
93 to: CGPoint(
94 x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y) + vertexSize.height)
95 )
96 path.closeSubpath()
97 path.move(to: CGPoint(x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y)))
98 path.addLine(
99 to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
100 path.closeSubpath()
fdb4633d 101 }.strokedPath(StrokeStyle(lineWidth: 2.0, lineCap: .butt))
77d0155b
RBR
102 }
103 }
5e8ff485
RBR
104}
105
e2c37ac1
RBR
106#Preview {
107 MapVertices(
108 mapSize: CGSize(width: 400.0, height: 400.0), vertexSize: CGSize(width: 25.0, height: 25.0),
109 vertices: [
110 Vertex(id: 0, label: "A Circle", position: CGPoint(x: 50.0, y: 50.0)),
111 Vertex(id: 1, label: "A Square", position: CGPoint(x: 10.0, y: 20.0), shape: .square),
112 Vertex(id: 2, label: "A triangle", position: CGPoint(x: 25, y: 32.0), shape: .triangle),
113 Vertex(id: 3, label: "An X", position: CGPoint(x: 70.0, y: 70.0), shape: .x),
114 ])
5e8ff485 115}